<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller\msjx;

use app\data\model\DataEnterpriseFreePolicy as DataNewsItem;
use app\data\model\DataNoticePushLog;
use app\data\model\DataPolicyItem;
use app\data\model\DataQiyeRzinfo;
use app\data\service\NoticeService;
use app\data\service\PolicyService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\helper\QueryHelper;
use think\admin\service\AdminService;
use think\facade\Db;

/**
 * 财务审核
 * Class Item
 * @package app\data\controller\news
 */
class FinancialReview extends Controller
{
    /**
     * 内容管理
     * @auths true
     * @menus true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->type = $this->get['type'] ?? 'index';
        DataNewsItem::mQuery($this->get)->layTable(function ()
        {
            $this->title = '财务审核';
        }, function (QueryHelper $query)
        {
            // $query->like('code,title')->equal('tj_status')->dateBetween('create_at');
            // $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0]);
            $query->with(['policy', 'item', 'enterprise']);//->with('item')->with('enterprise');
            $admin = AdminService::getUserId();
            if ($admin != 10000) {
                $query->whereRaw("admin_id = {$admin}");
            }
        });
    }

    /**
     * 列表数据处理
     * @param  array  $data
     */
    protected function _page_filter(array &$data)
    {

        foreach ($data as &$item) {
            $item['policy_item_name'] = $item['item']['content'] ?? '';
            $item['policy_name']      = $item['policy']['title'] ?? '';
            $item['enterprise_name']  = $item['enterprise']['qymc'] ?? '';
        }


    }

    /**
     * 添加文章内容
     * @auths true
     */
    public function add()
    {
        $this->title = '添加内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 编辑文章内容
     * @auths true
     */
    public function edit()
    {
        $this->title = '编辑内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 表单数据处理
     * @param  array  $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _edit_form_filter(array &$data)
    {

        if ($this->request->isGet()) {
            $request  = $this->request->all();
            $this->id = $request['id'];
        } else {
        }
    }

    /**
     * 表单数据处理
     * @param  array  $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {

        if ($this->request->isGet()) {
            if (!empty($data['free_time'])) {
                $data['free_time'] = strtotime($data['free_time']);
                $data['free_time'] = date('Y-m-d', $data['free_time']);
            }
        } else {
            //获取当前添加内容的管理员id
            $data['admin_id'] = AdminService::getUserId();
        }
    }

    /**
     * 表单结果处理
     * @param  boolean  $state
     * @param           $data
     * @throws \think\db\exception\DbException
     */
    protected function _form_result(bool $state, $data)
    {
        if ($state) {
            //设置已兑换

            if ($data['free_time'] && $data['money']) {
                /**
                 * @var DataNewsItem $info
                 */
                $info = DataNewsItem::query()->find([$data['id']]);
                $info->setStatusSuccess();
                //查询企业认证用户
                $userIds = DataQiyeRzinfo::query()->where('qrxxk_id', $info->enterprise_id)->select()->toArray();
                if ($userIds) {
                    $userIds    = array_column($userIds, 'user_id');
                    $noticeList = [];
                    foreach ($userIds as $userId) {
                        $noticeList[] = [
                            'user_id' => $userId,
                            'title'   => $info->item->content,
                            'content' => '',
                            'type'    => DataNoticePushLog::NOTICE_TYPE_FREE_POLICY,
                        ];
                    }
                    NoticeService::instance()->batchCreateNotice($noticeList);
                }
            }

            //发送企业通知

            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改文章状态
     */
    public function state()
    {
        DataNewsItem::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除文章内容
     * @auths true
     */
    public function remove()
    {
        DataNewsItem::mDelete();
    }

    /**
     * 文章内容选择
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        $this->get['status'] = 1;
        $this->index();
    }

    public function getQybqList()
    {
        $biaoqian = Db::name('data_qiye_biaoqian')->where('deleted', 0)->select()->toArray();
        return_msg(200, 'success', $biaoqian);
    }

    public function shenhe()
    {
        $id = input('id');
        Db::name("data_msjx")->where('id', $id)->update(['tj_status' => 2]);
        Db::name("data_msjx_user")->where('msjx_id', $id)->update(['tj_status' => 2]);
        $this->success('操作成功！', 'javascript:history.back()');
    }

    public function gongshi()
    {
        $id = input('id');
        Db::name("data_msjx")->where('id', $id)->update(['tj_status' => 3]);
        Db::name("data_msjx_user")->where('msjx_id', $id)->update(['tj_status' => 3]);
        $this->success('操作成功！', 'javascript:history.back()');
    }


    public function luoshi()
    {
        $id = input('id');
        Db::name("data_msjx")->where('id', $id)->update(['tj_status' => 4]);
        Db::name("data_msjx_user")->where('msjx_id', $id)->update(['tj_status' => 4]);
        $this->success('操作成功！', 'javascript:history.back()');
    }
}